package cn.jsc.pay.service.impl;

import cn.jsc.basic.exception.BusinessException;
import cn.jsc.basic.service.impl.BaseServiceImpl;
import cn.jsc.pay.domain.AlipayInfo;
import cn.jsc.pay.domain.PayBill;
import cn.jsc.pay.mapper.AlipayInfoMapper;
import cn.jsc.pay.mapper.PayBillMapper;
import cn.jsc.pay.service.IPayBillService;
import cn.jsc.pay.utils.AlipayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    /**
     * 支付方式  方便以后扩展
     * @param payBill
     * @return
     */
    @Override
    public String payRequest(PayBill payBill) {
        if(payBill == null){
            throw new BusinessException("您的支付单不存在，请重新创建支付单！");
        }
        switch (payBill.getPayChannel().intValue()){ //比int小的【byte，short，char int】 String
            case 0:
                System.out.println("银联支付");
                break;
            case 1:
                System.out.println("微信支付");
                break;
            case 2:
                AlipayInfo alipayInfo = alipayInfoMapper.loadByShopId(payBill.getShop_id());
                String payData = AlipayUtil.pay(alipayInfo, payBill);//用支付宝去支付一个支付单
                return payData;
        }
        return null;
    }

    /**
     * 通过paysn查询PayBill对象
     * @param paySn
     * @return
     */

    @Override
    public PayBill loadByPaySn(String paySn) {
        return payBillMapper.loadByUnionPaySn(paySn);
    }
}
